package com.dmyang.controller;

import com.dmyang.common.pojo.BootstrapTableResult;
import com.dmyang.common.pojo.HyResult;
import com.dmyang.entity.HyArticle;
import com.dmyang.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by brss on 2017/8/7.
 */
@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 正式发布一篇文章
     *
     * @param article
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public HyResult addArticle(HyArticle article) {
        try {
            //请求的是正式发表文章，所以这里是1
            article.setStatus((byte) 1);
            HyResult result = articleService.addArticle(article);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(500, "插入失败！");
        }
    }
    /**
     * 保存一篇文章为草稿
     *
     * @param article
     * @return
     */
    @RequestMapping("/addDraft")
    @ResponseBody
    public HyResult addArticleDarft(HyArticle article) {
        try {
            //请求的是正式发表文章，所以这里是1
            article.setStatus((byte) 0);
            HyResult result = articleService.addArticle(article);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(500, "插入失败！");
        }
    }

    /**
     * 列出所有的文章
     * @param belong 属于那个用户
     * @param offset 从第多少条开始展示
     * @param limit 每页展示多少条
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public BootstrapTableResult showArticleList(Integer belong,Integer offset,Integer limit){
        try {

            BootstrapTableResult result = articleService.showArticleList(1, offset, limit);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new BootstrapTableResult(0,null);
        }
    }

    /**
     * 删除文章（可同时删除多个，用逗号隔开）
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public HyResult deleteArticle(String ids){
        try {
            HyResult result = articleService.deleteArticle(ids);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(200,"删除失败！");
        }
    }

    /**
     * 修改一篇文章，并直接发表
     * @param article
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public HyResult modifyArticle(HyArticle article){
        try {
            article.setStatus((byte)1);
            HyResult result = articleService.modifyArticle(article);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(500,"修改失败！");
        }
    }

    /**
     * 修改一篇文章，并转为草稿
     * @param article
     * @return
     */
    @RequestMapping("/modifyDraft")
    @ResponseBody
    public HyResult modifyArticleDraft(HyArticle article){
        try {
            article.setStatus((byte)0);
            HyResult result = articleService.modifyArticle(article);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(500,"修改失败！");
        }
    }

}